package com.ruoyi.sunshine.file.impl;

import com.ruoyi.sunshine.file.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {

    private static Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);

    /**
     * 获得文件信息
     * @param inputStream
     * @return
     */
    public byte[] getByteByInputStream(InputStream inputStream,String encode) {
        try{
            byte[] b= FileCopyUtils.copyToByteArray(inputStream);
            return b;
        }catch(Exception e){
            logger.error("[getByteByInputStream] error",e);
            throw new RuntimeException("读取文件失败"+e.getMessage());
        }
    }

    /**
     * 获得文件信息
     * @param inputStream
     * @return
     */
    public List<String> getFileInfoByInputStream(InputStream inputStream,String encode) {
        try{
            Reader info=new InputStreamReader(inputStream,encode);
            BufferedReader reader = new BufferedReader(info);
            String line="";
            List<String> content=new ArrayList<>();
            while((line=reader.readLine())!=null){
                content.add(line);
            }
            reader.close();
            info.close();
            return content;
        }catch(Exception e){
            logger.error("[getFileInfoByInputStream] error",e);
            throw new RuntimeException("读取文件失败"+e.getMessage());
        }
    }

    /**
     * 获得文件信息
     * @param inputStream
     * @return
     */
    public List<String> getFileInfoByInputStream(InputStream inputStream) {
        try{
            Reader info=new InputStreamReader(inputStream);
            BufferedReader reader = new BufferedReader(info);
            String line="";
            List<String> content=new ArrayList<>();
            while((line=reader.readLine())!=null){
                content.add(line);
            }
            reader.close();
            info.close();
            return content;
        }catch(Exception e){
            logger.error("[getFileInfoByInputStream] error",e);
            throw new RuntimeException("读取文件失败"+e.getMessage());
        }
    }

    /**
     * 写文件
     * @param path
     * @param content
     * @return
     */
    public boolean writeFileInfo(String path,List<String> content) {
        PrintWriter writer=null;
        boolean flag = false;
        try{
            writer=new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),"UTF-8")));
            for(String bean:content){
                writer.write(bean);
                writer.write("\r\n");
            }
//            writer.close();
            flag= true;
        }catch(Exception e){
            logger.error("[writeFileInfo] error",e);
//            throw new RuntimeException("写文件失败"+e.getMessage());
            flag=false;
        }finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }
    /**
     * 创建文件
     * @param path
     * @return
     */
    public boolean createFile(String path) {
        try{
            File file=new File(path);
            if(!file.exists()){
                file.createNewFile();
            }else{
                throw new RuntimeException("文件已存在");
            }
            return true;
        }catch(Exception e){
            logger.error("[createFile] error",e);
            throw new RuntimeException("创建文件失败"+e.getMessage());
        }
    }

    /**
     * 删除文件
     * @param path
     * @return
     */
    public boolean deleteFile(String path) {
        try{
            File file=new File(path);
            if(file.exists()&&file.isFile()){
                file.delete();
            }else{
                throw new RuntimeException("没找到要删除的文件");
            }
            return true;
        }catch(Exception e){
            logger.error("[deleteFile] error",e);
            throw new RuntimeException("删除文件失败"+e.getMessage());
        }
    }

}
